package com.example.flow.biz.interceptor.approval;

import com.example.flow.dao.FlowApproveApplyDao;
import com.example.flow.dao.model.FlowApproveApply;
import com.example.flow.dao.model.FlowTask;
import com.example.flow.enums.EnumApproveStatus;
import com.example.flow.flow.FlowNodeInterceptor;
import com.example.flow.flow.core.Execution;
import com.example.flow.utils.FlowUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 下一节点审批拦截器
 *
 * @author safe
 * @date 2021/1/31
 */
@Service
public class NextCommonApproveInterceptor implements FlowNodeInterceptor {
    private static Logger log = LoggerFactory.getLogger(NextCommonApproveInterceptor.class);

    @Autowired
    private FlowApproveApplyDao flowApproveApplyDao;

    @Override
    public void intercept(Execution execution) {
        String orderId = FlowUtil.getOrderId(execution);
        String applyId = FlowUtil.getApplyId(execution);
        log.info("任务[{}]进入NextCommonApproveInterceptor", orderId);
        FlowTask task = execution.getTask();
        FlowApproveApply approveApply = flowApproveApplyDao.queryOne(applyId, null);

        approveApply.setStatus(EnumApproveStatus.DOING.getCode());
        approveApply.setOrderId(orderId);
        approveApply.setCurrentTaskId(task.getTaskId());
        approveApply.setCurrentNodeCode(task.getTaskName());
        approveApply.setCurrentNodeName(task.getTaskName());
        approveApply.setCurrentOperatorId(task.getOperator());
        approveApply.setUpdateTime(new Date());
        flowApproveApplyDao.updateByExample(approveApply);
    }
}
